<template>
	<view class="index">
		<view class="tu">
			<image src="../../static/images/other/production.png"></image>
		</view>

		<view class="shu">{{userInfo.productTimes}}</view>
		<view class="chongzhi">
			<view class="kuang">
				<view class="kuang1" @tap="diyi()" :style="'background-color:'+color1">
					<view>
						<view class="yi">0.5元</view>
						<view class="er">1次</view>
					</view>
				</view>
				<view class="kuang2" @tap="dier()" :style="'background-color:'+color2">
					<view>
						<view class="yi">1元</view>
						<view class="er">4次</view>
					</view>
				</view>
				<view class="kuang3" @tap="disang()" :style="'background-color:'+color3">
					<view>
						<view class="yi">2元</view>
						<view class="er">10次</view>
					</view>
				</view>
			</view>
			<view class="kuang">
				<view class="kuang1" @tap="disi()" :style="'background-color:'+color4">
					<view>
						<view class="yi">3元</view>
						<view class="er">20次</view>
					</view>
				</view>
				<view class="kuang2" @tap="diwu()" :style="'background-color:'+color5">
					<view>
						<view class="yi">5元</view>
						<view class="er">50次</view>
					</view>
				</view>
				<view class="kuang2" @tap="diliu()" :style="'background-color:'+color6">
					<view>
						<view class="yi">99元</view>
						<view class="er">99999次</view>
					</view>
				</view>
			</view>
		</view>

		<view class="text">
			<view @tap="gopay()" class="chongtext" hover-class="chongtext2">立即充值</view>
		</view>

	</view>

	</view>


</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		http
	} from '../../utils/http.js'
	export default {
		data() {
			return {
				color1: 'white',
				color2: 'white',
				color3: 'white',
				color4: 'white',
				color5: 'white',
				color6: 'white',
				chongzhimoney: 0
			};
		},
		computed: {
			...mapState('m_user', ['userInfo', 'token'])
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken']),
			diyi() {
				let color1 = this.color1
				if (color1 == 'white') {
					this.chongzhimoney = 50
					this.color1 = '#f7e4a7',
						this.color2 = 'white',
						this.color3 = 'white',
						this.color4 = 'white',
						this.color5 = 'white',
						this.color6 = 'white'
				} else {
					this.chongzhimoney = 0
					this.color1 = 'white'
				}

			},
			dier() {
				let color2 = this.color2
				if (color2 == 'white') {
					this.chongzhimoney = 100
					this.color2 = '#f7e4a7',
						this.color1 = 'white',
						this.color3 = 'white',
						this.color4 = 'white',
						this.color5 = 'white',
						this.color6 = 'white'
				} else {
					this.chongzhimoney = 0
					this.color2 = 'white'
				}

			},
			disang() {
				let color3 = this.color3
				if (color3 == 'white') {
					this.chongzhimoney = 200
					this.color3 = '#f7e4a7',
						this.color1 = 'white',
						this.color2 = 'white',
						this.color4 = 'white',
						this.color5 = 'white',
						this.color6 = 'white'
				} else {
					this.chongzhimoney = 0
					this.color3 = 'white'
				}


			},
			disi() {
				let color4 = this.color4
				if (color4 == 'white') {
					this.chongzhimoney = 300
					this.color4 = '#f7e4a7',
						this.color1 = 'white',
						this.color3 = 'white',
						this.color2 = 'white',
						this.color5 = 'white',
						this.color6 = 'white'
				} else {
					this.chongzhimoney = 0
					this.color4 = 'white'
				}

			},
			diwu() {
				let color5 = this.color5
				if (color5 == 'white') {
					this.chongzhimoney = 500
					this.color5 = '#f7e4a7',
						this.color1 = 'white',
						this.color3 = 'white',
						this.color4 = 'white',
						this.color2 = 'white',
						this.color6 = 'white'
				} else {
					this.chongzhimoney = 0
					this.color5 = 'white'
				}

			},
			diliu() {
				let color6 = this.color6
				if (color6 == 'white') {
					this.chongzhimoney = 9900
					this.color6 = '#f7e4a7',
						this.color1 = 'white',
						this.color3 = 'white',
						this.color4 = 'white',
						this.color5 = 'white',
						this.color2 = 'white'
				} else {
					this.chongzhimoney = 0
					this.color6 = 'white'
				}
			},
			gopay() {
				var that = this
				let money = this.chongzhimoney
				if (money == 0) {
					uni.showToast({
						icon: 'none',
						title: '还没有选择充值数额',
					})
				} else {
					that.chongzhi(money)
				}

			},
			async chongzhi(money) {
				let token = this.token.data
				let moneyfen = Number(money * 1)
				const res = await http({
					url: '/recharge/order/prePay?money=' + moneyfen,
					method: 'POST',
					header: {
						"Authorization": token,
						'content-type': "application/x-www-form-urlencoded",
					},
				})

				if (res.code == 200) {
					this.payMoney(res.data)
				} else {
					uni.showToast({
						icon: 'none',
						title: res.message,
						duration: 2000
					})
				}


			},
			payMoney(e) {
				var that = this
				uni.requestPayment({
					timeStamp: e.timeStamp, //记住，这边的timeStamp一定要是字符串类型的，不然会报错
					nonceStr: e.nonceStr,
					package: e.package,
					signType: e.signType,
					paySign: e.paySign,
					success: function(event) {
						// console.log(event)
						uni.showToast({
							title: '支付成功',
							icon: 'success',
							duration: 2000
						})
						if (event.errMsg.indexOf('ok') != -1) {
							// 通知用户状况
							http({
								url: '/recharge/order/notify/' + e.orderId + '/' + 1,
								method: 'POST',
								header: {
									"Authorization": that.token.data,
									'content-type': "application/x-www-form-urlencoded",
								},
							}).then(r => {
								if (r.code == 200) {
									that.updateUserInfo(r.data)
									setTimeout(()=>{
										uni.navigateBack({
											delta:1
										})
									},1000)
								} else {
									uni.showToast({
										icon: 'none',
										title: r.message,
										duration: 2000
									})
								}
							})

						}

					},

					fail: function(error) {
						uni.showToast({
							title: '支付失败',
							icon: "error",
							duration: 2000
						});
						// 通知用户状况
						http({
							url: '/recharge/order/notify/' + e.orderId + '/' + 0,
							method: 'POST',
							header: {
								"Authorization": that.token.data,
								'content-type': "application/x-www-form-urlencoded",
							},
						})
					}
				});
			},
		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}
</style>
<style lang="scss" scoped>
	.index {
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		/* background-color: #f2b800; */
		height: 100vh;
		box-sizing: border-box;
	}

	.tu {
		width: 100%;
		height: 400rpx;
		border: #fff;
		text-align: center;
		margin-top: 100rpx;
		/* background-color: aqua; */
	}

	.tu image {
		position: relative;
		width: 400rpx;
		height: 400rpx;
		margin: 0 auto;
	}

	.shu {
		text-align: center;
		font-size: 50rpx;
		color: slategrey;
		margin-top: 40rpx;
	}

	.chongzhi {
		display: flex;
		flex-direction: column;
		justify-content: center;
		width: 100%;
		height: 300rpx;
	}

	.kuang {
		display: flex;
		flex-direction: row;
		justify-content: center;
		width: 100%;
		height: 82rpx;
		margin-top: 40rpx;
	}

	.kuang1 {
		display: flex;
		flex-direction: column;
		justify-content: center;
		width: 100rpx;
		height: 80rpx;
		color: #f2b800;
		/* font-size: 20rpx; */
		text-align: center;
		border-radius: 20rpx;
		border: 1px solid #f2b800;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	.kuang2 {
		display: flex;
		flex-direction: column;
		justify-content: center;
		width: 100rpx;
		text-align: center;
		height: 80rpx;
		color: #f2b800;
		margin-left: 50rpx;
		background-color: #fff;
		border-radius: 20rpx;
		border: 1px solid #f2b800;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	.kuang3 {
		display: flex;
		flex-direction: column;
		justify-content: center;
		width: 100rpx;
		height: 80rpx;
		text-align: center;
		color: #f2b800;
		margin-left: 50rpx;
		border-radius: 20rpx;
		background-color: #fff;
		border: 1px solid #f2b800;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	.kuang32 {
		display: flex;
		flex-direction: column;
		justify-content: center;
		width: 100rpx;
		height: 80rpx;
		text-align: center;
		color: #f2b800;
		margin-left: 50rpx;
		border-radius: 20rpx;
		background-color: #f7e4a7;
		border: 1px solid #f2b800;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	.yi {
		font-size: 30rpx;
	}

	.er {
		font-size: 20rpx;
	}

	/* 按钮 */
	.text {
		width: 100%;
		height: 100rpx;
		margin-top: 10rpx;
	}

	.chongtext {
		width: 260rpx;
		height: 80rpx;
		font-size: 32rpx;
		line-height: 80rpx;
		text-align: center;
		color: white;
		margin: 0 auto;
		border-radius: 2em;
		border: 1px solid #f7f5f5;
		background-color: #3ea2ff;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	.chongtext2 {
		width: 260rpx;
		height: 80rpx;
		font-size: 34rpx;
		line-height: 80rpx;
		text-align: center;
		color: white;
		margin: 0 auto;
		border-radius: 2em;
		border: 1px solid #f7f5f5;
		background-color: #3ea2ff;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	/* 弹窗样式 */
	.model {
		position: fixed;
		width: 100%;
		height: 100%;
		background: #000;
		z-index: 999;
		opacity: 0.5;
		top: 0;
		left: 0;
	}

	.modalDlg {
		width: 70%;
		position: fixed;
		top: 300rpx;
		left: 0;
		right: 0;
		z-index: 9999;
		margin: 0 auto;
		padding: 55rpx;
		background-color: #fff;
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.windowRow {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding-bottom: 15rpx;
		width: 100%;
	}

	.userTitle {
		font-size: 42rpx;
	}

	.back {
		text-align: center;
		color: #3ea2ff;
		font-size: 40rpx;
	}

	.wishName {
		width: 100%;
		justify-content: center;
		flex-direction: row;
		display: flex;
		margin: 40rpx 0rpx;
	}

	.wish_put {
		width: 100%;
		border: #ededef 1rpx solid;
		background-color: #f6f7fb;
		border-radius: 12rpx;
		padding: 30rpx 20rpx;
		margin: 20rpx 0rpx;
	}

	.holder_cls {
		color: #909399;
	}

	.wishbnt {
		background-color: #3ea2ff;
		text-align: center;
		border-radius: 50rpx;
		padding: 25rpx;
		color: white;
	}
</style>
